﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.DynamicData;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using NotAClue.ComponentModel.DataAnnotations;
using C1.Web.Wijmo.Juice.WijTabs;

namespace DD_Extensions
{
    public partial class WijmoTabsTemplate : System.Web.DynamicData.EntityTemplateUserControl
    {
        protected override void OnLoad(EventArgs e)
        {
            var groupAttribute = Table.GetAttribute<GroupNamesAttribute>();
            if (groupAttribute == null)
                throw new InvalidOperationException("A GroupsAttribute is required for AJAX tab group to work.");

            var row = new HtmlTableRow();
            var td = new HtmlTableCell();
            row.Controls.Add(td);
            this.Controls.Add(row);
            // create tab container to hold each children column
            var panel = new Panel();
            panel.ID = "tabContainer_" + Table.Name;

            // add a tab panel for each children table
            var unorderedList = new HtmlGenericControl("ul");

            // SortedList of tabs
            var tabs = new SortedList<String, HtmlGenericControl>();

            // add DIV for each group
            foreach (var gi in groupAttribute.Groups)
            {
                var groupName = gi.Value;

                var tabDiv = new HtmlGenericControl("div");
                tabDiv.ClientIDMode = ClientIDMode.Static;
                tabDiv.ID = String.Format("{0}-{1}", Table.Name, groupName).Replace(" ", "-");

                // get columns for this group
                var columns = from c in Table.GetScaffoldColumns(Mode, ContainerType)
                              where c.GetAttributeOrDefault<GroupAttribute>().Index == gi.Key
                              orderby c.GetAttributeOrDefault<DisplayAttribute>().GetOrder()
                              select c;

                // add table for this tabs fields
                var htmlTable = new HtmlTable();
                htmlTable.Attributes.Add("class", "DDDetailsTable");
                htmlTable.CellPadding = 6;
                htmlTable.Attributes.Add("Name", groupName);

                // add fields
                foreach (MetaColumn column in columns)
                {
                    // new row
                    var htmlRow = new HtmlTableRow();
                    htmlTable.Controls.Add(htmlRow);

                    // add header cell
                    var tdHeader = new HtmlTableCell();
                    tdHeader.Attributes.Add("class", "DDLightHeader");
                    tdHeader.InnerText = column.DisplayName;
                    // add cell to row
                    htmlRow.Controls.Add(tdHeader);

                    // add data cell
                    var tdData = new HtmlTableCell();
                    // get field template
                    var dynamicControl = new DynamicControl()
                        {
                            Mode = Mode,
                            DataField = column.Name,
                            ValidationGroup = this.ValidationGroup
                        };
                    // add field template to cell
                    tdData.Controls.Add(dynamicControl);
                    // add cell to row
                    htmlRow.Controls.Add(tdData);
                }

                // add the DynamicControl to the tab panel
                tabDiv.Controls.Add(htmlTable);

                // add the tab to list
                tabs.Add(groupName, tabDiv);
            }

            foreach (var gi in groupAttribute.Groups)
            {
                var groupName = gi.Value;

                // new 'LI'
                var tabLi = new HtmlGenericControl("li");

                // new hyperlink
                var hyperlink = new Literal();
                hyperlink.Text = String.Format("<a href=\"#{0}\" >{1}</a>", tabs[groupName].ClientID, groupName);

                // add hyperlink to 'LI'
                tabLi.Controls.Add(hyperlink);

                // add LIs items to UL
                unorderedList.Controls.Add(tabLi);
            }

            // add UL to panel
            panel.Controls.Add(unorderedList);

            // add DIVs to panel
            foreach (var gi in groupAttribute.Groups)
            {
                var groupName = gi.Value;
                panel.Controls.Add(tabs[groupName]);
            }

            // add the panel to the page
            td.Controls.Add(panel);

            // associate the panel with the Wijmo tabs
            var wijmoTabs1 = new WijTabs()
                {
                    ID = "wijmoTabs1",
                    TargetControlID = panel.ID
                };

            //wijmoTabs1.Alignment = Alignment.Top;

            //wijmoTabs1.Collapsible = true;
            //wijmoTabs1.Sortable = true;

            //wijmoTabs1.Event = "mouseover";
            wijmoTabs1.Scrollable = true;

            // add Wijmo tabs to the page
            td.Controls.Add(wijmoTabs1);

            wijmoTabs1.ShowOption.Duration = 300;
            wijmoTabs1.ShowOption.Blind = true;
            wijmoTabs1.ShowOption.Fade = true;

            wijmoTabs1.HideOption.Duration = 300;
            wijmoTabs1.HideOption.Blind = true;
            wijmoTabs1.HideOption.Fade = true;
        }

        protected void Label_PreRender(object sender, EventArgs e)
        {
            Label label = (Label)sender;
            DynamicControl dynamicControl = (DynamicControl)label.FindControl("DynamicControl");
            FieldTemplateUserControl ftuc = dynamicControl.FieldTemplate as FieldTemplateUserControl;

            if (ftuc != null && ftuc.DataControl != null)
                label.AssociatedControlID = ftuc.DataControl.GetUniqueIDRelativeTo(label);
        }

        public class _NamingContainer : Control, INamingContainer { }
    }
}